package au.com.handsunited.web.controller.entity;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import au.com.handsunited.model.BaseEntity;
import au.com.handsunited.model.relationship.Relationship;
import au.com.handsunited.service.EntityService;
import au.com.handsunited.web.session.SessionSupport;

/**
 * @author arunda
 *
 */
public class ChangeRelationshipStatusController extends MultiActionController {


	private EntityService entityService;

	private static Logger logger = LoggerFactory.getLogger(ChangeRelationshipStatusController.class);


	public ChangeRelationshipStatusController() {
	}


	public ChangeRelationshipStatusController(Object delegate) {
		super(delegate);
	}



	public ModelAndView approve(HttpServletRequest request, HttpServletResponse response, HttpSession session) {

		String withEntityId = request.getParameter("with");
		BaseEntity activeEntity = SessionSupport.getCurrentlyActiveEntity(request);
		if ((activeEntity != null) && StringUtils.isNotBlank(withEntityId)) {
			BaseEntity withEntity = entityService.getEntity(withEntityId);
			if (withEntity != null) {
				Relationship rel1 = activeEntity.findRelationshipWith(withEntity);
				if (rel1 != null) {
					rel1.approveRelationship();
				} else {
					//	log it
				}
				Relationship rel2 = withEntity.findRelationshipWith(activeEntity);
				if (rel2 != null) {
					rel2.approveRelationship();
				}
				SessionSupport.setCurrentlyActiveEntity(request, entityService.updateEntity(activeEntity, activeEntity));
				//	TODO this needs to be changed to just update the new relationship and nothing else
				entityService.updateEntity(withEntity, withEntity);
			}
		}

		ModelAndView mav = new ModelAndView("redirect:/user/profile.html");
		return mav;
	}



	public ModelAndView reject(HttpServletRequest request, HttpServletResponse response, HttpSession session) {

		String withEntityId = request.getParameter("with");
		BaseEntity activeEntity = SessionSupport.getCurrentlyActiveEntity(request);
		if ((activeEntity != null) && StringUtils.isNotBlank(withEntityId)) {
			BaseEntity withEntity = entityService.getEntity(withEntityId);
			if (withEntity != null) {
				Relationship rel1 = activeEntity.findRelationshipWith(withEntity);
				if (rel1 != null) {
					rel1.rejectRelationship();
				} else {
					//	log it
				}
				Relationship rel2 = withEntity.findRelationshipWith(activeEntity);
				if (rel2 != null) {
					rel2.rejectRelationship();
				}
				SessionSupport.setCurrentlyActiveEntity(request, entityService.updateEntity(activeEntity, activeEntity));
				//	TODO this needs to be changed to just update the new relationship and nothing else
				entityService.updateEntity(withEntity, withEntity);
			}
		}

		ModelAndView mav = new ModelAndView("redirect:/user/profile.html");
		return mav;
	}


	public void setEntityService(EntityService entityService) {
		this.entityService = entityService;
	}


}

